//
//  RegisterViewController.m
//  ManageMoney
//
//  Created by epwk on 15/9/15.
//  Copyright (c) 2015年 db. All rights reserved.
//

#import "RegisterViewController.h"
#import "zhuceViewController.h"
#import "UserAgreementController.h"



@interface RegisterViewController ()<UIScrollViewDelegate>
{
    NSTimer *countDownTimer;
    int secondsCountDown; //60秒倒数计时
}
@property (strong, nonatomic) IBOutlet UIButton *btnYanzhengma;//验证码btn
@property (weak, nonatomic) IBOutlet UITextField *registerCodeField;//注册码
@property (weak, nonatomic) IBOutlet UITextField *userNameField;//手机号码
@property (weak, nonatomic) IBOutlet UITextField *pwdField;//登录密码
@property (weak, nonatomic) IBOutlet UITextField *pwdConfirmationField;//登录 密码确认

@property (strong, nonatomic) IBOutlet UIScrollView *scrollview;
@property (strong, nonatomic) IBOutlet UIButton *selectedbtn;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"注册";
    self.scrollview.delegate = self;
    
    [self.scrollview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollviewclick:)]];
}


-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.scrollview.contentSize = CGSizeMake(0, 700);
}


-(void)scrollviewclick:(UITapGestureRecognizer *)tapgestur
{
    [self.view endEditing:YES];
}
//发送短信
- (IBAction)yanzhengma:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([_userNameField.text.trim isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"手机号不能为空"];
        return;
    }
    
    NSString * regex = @"(^1[0-9]{10}$)";
    NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if (!([pred evaluateWithObject:_userNameField.text.trim])) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }
    
    [SVProgressHUD show];
    NSDictionary *pramas  = [[NSDictionary alloc]init];
    pramas = @{@"phone":_userNameField.text,@"appCode":@"B001"};
    
  
    [SVProgressHUD show];
    NSString *requestRegisterMessageUrl = [@"" stringByAppendingFormat:@"%@%@",hrtxUrl,@"cust/sendSm.do"];
    [HWHttpTool post:requestRegisterMessageUrl params:pramas success:^(id json) {
        [SVProgressHUD dismiss];
        NSDictionary *dic = json;
        if ([dic[@"responseCode"] intValue] == 200) {
            [SVProgressHUD showSuccessWithStatus:dic[@"responseMessage"]];
            //            //发送短信
            //禁用短信发送按钮，一分钟后再次开启
            secondsCountDown = 60;//60秒倒计时
            countDownTimer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
            
            
        }else
        {
            [SVProgressHUD showErrorWithStatus:dic[@"responseMessage"]];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [SVProgressHUD dismiss];
    }];
    


}

-(void)timeFireMethod{
    secondsCountDown--;
    if(secondsCountDown<0){
        [countDownTimer invalidate];
        [_btnYanzhengma setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_btnYanzhengma setBackgroundColor:[UIColor blueColor]];
        [_btnYanzhengma setEnabled:YES];
    }else{
        [_btnYanzhengma setEnabled:NO];
        [_btnYanzhengma setBackgroundColor:[UIColor lightGrayColor]];
        if([SVProgressHUD getSystemVersion ]>=8){
            [_btnYanzhengma setTitle:[NSString stringWithFormat:@"%i秒后再获取",secondsCountDown] forState:UIControlStateNormal];
        }else{
            [_btnYanzhengma setTitle:@"请稍后片刻……" forState:UIControlStateNormal];
        }
    }
}

- (IBAction)selectedbtn:(id)sender {
    _selectedbtn.selected = !_selectedbtn.selected;
}
- (IBAction)xieyi:(id)sender {
    
    UserAgreementController *ctr = [[UserAgreementController alloc] initWithNibName:@"UserAgreementController" bundle:nil];
    [self.navigationController pushViewController:ctr animated:YES];
}

#pragma mark - 注册
- (IBAction)zhuceclick:(id)sender {
    [self TextfieldresignFirstResponder];
    
    if ([_userNameField.text.trim isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"手机号不能为空"];
        return;
    }

    NSString * regex = @"(^1[0-9]{10}$)";
    NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if (!([pred evaluateWithObject:_userNameField.text.trim])) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }
    
    if ([_registerCodeField.text.trim isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"验证码不能为空"];
        return;
    }
    
    NSString * regex1 = @"(^[0-9]{5,6}$)";
    NSPredicate * pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex1];
    if (![pred1 evaluateWithObject:_registerCodeField.text.trim]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的验证码"];
        return;
    }
    

    if (!(_pwdField.text.trim.length >5&&_pwdField.text.trim.length<16)) {
        [SVProgressHUD showErrorWithStatus:@"请输入6-16位密码"];
        return;
    }
    if (![_pwdField.text.trim isEqualToString:_pwdConfirmationField.text.trim]) {
        [SVProgressHUD showErrorWithStatus:@"您输入的密码不一致"];
        return;
    }
    if (_selectedbtn.selected ==NO) {
        [SVProgressHUD showErrorWithStatus:@"请遵守协议"];
        return;
    }
    
    NSDictionary *pramas  = [[NSDictionary alloc]init];
    pramas = @{@"phone":_userNameField.text,@"password":_pwdConfirmationField.text,@"appCode":@"B001",@"smCode":_registerCodeField.text};

    [self requestRegisterWithParams:pramas];
    
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self TextfieldresignFirstResponder];
}

-(void)TextfieldresignFirstResponder
{
    [self.registerCodeField resignFirstResponder];
    [self.userNameField resignFirstResponder];
    [self.pwdConfirmationField resignFirstResponder];
    [self.pwdField resignFirstResponder];
}

#pragma mark - RequestAction 注册
-(void)requestRegisterWithParams:(NSDictionary *)pramas
{
    
    [SVProgressHUD show];
    NSString *RegisterUrl = [@"" stringByAppendingFormat:@"%@%@",hrtxUrl,@"cust/register.do"];
    [HWHttpTool post:RegisterUrl params:pramas success:^(id json) {
        [SVProgressHUD dismiss];
        NSDictionary *dic = json;
        if ([dic[@"responseCode"] intValue] == 200) {
            [SVProgressHUD showSuccessWithStatus:@"注册成功"];
            
            [UserDefaults setObject:pramas[@"userName"] forKey:kRememberuserNmae];
            [UserDefaults setObject:pramas[@"password"] forKey:kRememberPSW];
            [UserDefaults synchronize];
            
            zhuceViewController *ctr = [[zhuceViewController alloc] initWithNibName:@"zhuceViewController" bundle:nil];
            ctr.zhuceStr = dic[@"responseObj"];
            [self.navigationController pushViewController:ctr animated:YES];
            
            
        }else
        {
            [SVProgressHUD showErrorWithStatus:dic[@"responseMessage"]];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [SVProgressHUD dismiss];
    }];
    
}



@end
